package com.cc.app.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.cc.app.view.TestView;

/**
 * checkbox
 */
public class CheckView extends TestView {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int radius = 250;

    private RectF rect = new RectF(-radius, -radius, radius, radius);

    private int gap = 300;

    private int status;

    private Path drawPath;

    private ValueAnimator animator;

    private boolean selected;

    private float ratio;

    private boolean reversed = false;

    public CheckView(Context context) {
        this(context, null);
    }

    public CheckView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initPaint();
        initListener();
        initAnimator();
    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(15);
    }

    private void initListener() {
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                selected = !selected;

                if (selected) {
                    if (status == 0)
                        status = 1;

                    animator.start();
                } else {
                    status = 0;
                }

                invalidate();
            }
        });
    }

    private void initAnimator() {
        animator = ValueAnimator.ofFloat(0, 1);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio = animation.getAnimatedFraction();
                invalidate();
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (status == 4)
                    return;

                if (status < 4) {
                    status++;
                }

                animator.start();
                invalidate();
            }
        });
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth() / 2, getHeight() / 2);

        switch (status) {
            case 0: // original
                paint0(canvas);
                break;
            case 1:
                paint1(canvas);
                break;
            case 2:
                paint2(canvas);
                break;
            case 3:
                paint3(canvas);
                break;
            case 4:
                paint4(canvas);
                break;
        }
    }

    private Path getTickPath() {
        Path path = new Path();
        path.moveTo(-100, 0);
        path.lineTo(0, 100);
        path.lineTo(130, -130);

        return path;
    }

    private void paint0(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);
        paint.setColor(Color.argb(200, 200, 200, 200));

        canvas.drawArc(rect, 0, 360, true, paint);
        canvas.drawPath(getTickPath(), paint);
    }

    private void paint1(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.YELLOW);
        canvas.drawArc(rect, 90, 360 * ratio, false, paint);
    }

    private void paint2(Canvas canvas) {
        float r = radius - 5;

        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(0, 0, r, paint);

        paint.setColor(Color.WHITE);
        canvas.drawCircle(0, 0, (1 - ratio) * r, paint);
    }

    private void paint3(Canvas canvas) {
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawCircle(0, 0, radius + 50 * ratio, paint);
    }

    private void paint4(Canvas canvas) {
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawCircle(0, 0, radius + 50 * (1 - ratio), paint);

        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAlpha((int) (ratio * 255));
        canvas.drawPath(getTickPath(), paint);

    }
}
